package com.woniuxy.woniuticket.statisticsmodule.controller;

import com.woniuxy.woniuticket.statisticsmodule.service.StatisticsService;
import com.woniuxy.woniuticket.statisticsmodule.vo.Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 赵晓东 on 2019/6/10/010.
 */
@Controller
@RequestMapping("/moviemanager")
public class ShowData {
    @Autowired
    private StatisticsService statisticsService;
    //一层映射
    @RequestMapping("/{path}")
    public String showView(@PathVariable String path){
        return "/moviemanager/"+path;
    }
    //三层层映射
    @RequestMapping("/{first}/{second}/{three}")
    public String showView1(@PathVariable String first,@PathVariable String second,@PathVariable String three){
        System.out.println("/moviemanager/"+first+"/"+second+"/"+three);
        return "/moviemanager/"+first+"/"+second+"/"+three;
    }
    //四层映射
    @RequestMapping("/{first}/{second}/{three}/{four}")
    public String showView1(@PathVariable String first,@PathVariable String second,@PathVariable String three,@PathVariable String four){
        System.out.println("/moviemanager/"+first+"/"+second+"/"+three+"/"+four);
        return "/moviemanager/"+first+"/"+second+"/"+three+"/"+four;
    }
    //五层映射
    @RequestMapping("/{first}/{second}/{three}/{four}/{five}")
    public String showView1(@PathVariable String first,@PathVariable String second,@PathVariable String three,@PathVariable String four,@PathVariable String five){
        System.out.println("/moviemanager/"+first+"/"+second+"/"+three+"/"+four+"/"+five);
        return "/moviemanager/"+first+"/"+second+"/"+three+"/"+four+"/"+five;
    }

    //营业额统计柱状图
    @RequestMapping("/turnover")
    @ResponseBody
    public Map<String, BigDecimal> turnover(Date startDay, Date endDay){
        return statisticsService.findturnover(startDay,endDay);
    }

    //营业额统计饼状图
    @RequestMapping("/turnoverPlus")
    @ResponseBody
    public List<Vo> turnoverPlus(Date startDay, Date endDay){
        List<Vo> list = new ArrayList<>();
        Map<String, BigDecimal> turnover = statisticsService.findturnover(startDay,endDay);
        Iterator<String> iterator = turnover.keySet().iterator();
        while (iterator.hasNext()){
            Vo vo = new Vo();
            String name = iterator.next();
            vo.setName(name);
            vo.setValue(turnover.get(name));
            list.add(vo);
        }
        return list;
    }


    //电影排片量柱状图
    @RequestMapping("/planMovie")
    @ResponseBody
    public Map<String, Integer> planMovie(){
        return statisticsService.findPlanMovie();
    }


    //电影排片量饼状图
    @RequestMapping("/planMoviePlus")
    @ResponseBody
    public List<Vo> planMoviePlus(){
        List<Vo> list = new ArrayList<>();
        Map<String, Integer> planMovie = statisticsService.findPlanMovie();
        Iterator<String> iterator = planMovie.keySet().iterator();
        while (iterator.hasNext()){
            Vo vo = new Vo();
            String name = iterator.next();
            vo.setName(name);
            vo.setValue(planMovie.get(name));
            list.add(vo);
        }
        return list;
    }

    //电影上座率
    @RequestMapping("/upperSeat")
    @ResponseBody
    public Map<String, Double> upperSeat(){
        return statisticsService.upperSeat();
    }

    //营业额统计饼状图
    @RequestMapping("/upperSeatPlus")
    @ResponseBody
    public List<Vo> upperSeatPlus(Date startDay, Date endDay){
        List<Vo> list = new ArrayList<>();
        Map<String, Double> turnover = statisticsService.upperSeat();
        Iterator<String> iterator = turnover.keySet().iterator();
        while (iterator.hasNext()){
            Vo vo = new Vo();
            String name = iterator.next();
            vo.setName(name);
            vo.setValue(turnover.get(name));
            list.add(vo);
        }
        return list;
    }
    // 日期转换器
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值
    }
}
